//
//  DB_BaseViewController.m
//  RacMVVM
//
//  Created by 崔博 on 2018/11/23.
//  Copyright © 2018年 崔博. All rights reserved.
//


#import "DB_BaseViewController.h"

@interface DB_BaseViewController ()
@property (nonatomic, strong, readwrite) DB_BaseViewModel *viewModel;

@end

@implementation DB_BaseViewController
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    DB_BaseViewController *viewController = [super allocWithZone:zone];
    @weakify(viewController)
    [[viewController
      rac_signalForSelector:@selector(viewDidLoad)]
     subscribeNext:^(id x) {
         @strongify(viewController)
         [viewController bindViewModel];
     }];
    
    return viewController;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
}
- (DB_BaseViewController *)initWithViewModel:(id)viewModel
{
    self = [super init];
    if (self) {
        self.viewModel = viewModel;
    }
    return self;
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self.viewModel.willDisappearSignal sendNext:nil];
    // Being popped, take a snapshot
    if ([self isMovingFromParentViewController]) {
        self.snapshot = [self.navigationController.view snapshotViewAfterScreenUpdates:NO];
    }
    
}
- (void)bindViewModel
{
    RAC(self,title) = RACObserve(self.viewModel, title);
    [[self.viewModel errors] subscribeNext:^(NSError * error) {
        NSLog(@"%@",error);
    }];
}
@end
